package com.example.wms;

/**
 * Class represents the list of items which are displayed
 * @author Andrew
 */
import java.util.ArrayList;
import java.util.Collection;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.EditText;
import com.example.classes.Database;
import com.example.classes.GlobalApplication;
import com.example.classes.Item;
import com.example.classes.Member;

public class itemListViewActivity extends Activity{

    private ListView listView;
    Database d;
    private EditText editText;
    private int txtLength = 0;
    private String[] listview_array;
    private ArrayAdapter<String> listAdapter;
    private ArrayList<String> array_sort= new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("itemListViewer","------  ZULU");
        GlobalApplication ga = (GlobalApplication)getApplicationContext();
        d = ga.getDb();
        setContentView(R.layout.itemlistview);
        Log.v("itemListViewer","------  VESPA");
        listview_array=populateList();
        Log.v("itemListViewer","------  XRAY  " + d.toString());
        listView = (ListView) findViewById(R.id.ListView01);
        editText = (EditText) findViewById(R.id.EditText01);

        String[] testArr = {"Staff","Cap","Belt"};

        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, testArr)); //listview_array

        Log.v("itemListViewer","------  ALPHA");
        editText.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s){
                txtLength = editText.getText().length();
                array_sort.clear();
                for(int i = 0; i < listview_array.length; i++){
                    if (txtLength <= listview_array[i].length()){
                        if(editText.getText().toString().equalsIgnoreCase((String)listview_array[i].subSequence(0,txtLength)))
                        {
                            array_sort.add(listview_array[i]);
                        }
                    }
                }
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){
            }
            public void onTextChanged(CharSequence s, int start, int before, int count){
                txtLength = editText.getText().length();
                array_sort.clear();
                for(int i = 0; i < listview_array.length; i++){
                    if (txtLength <= listview_array[i].length()){
                        if(editText.getText().toString().equalsIgnoreCase((String)listview_array[i].subSequence(0,txtLength)))
                        {
                            array_sort.add(listview_array[i]);
                        }
                    }
                }
            }
        });
        Log.v("itemListViewer","------  DELTA");
        listAdapter = new ArrayAdapter<String>(itemListViewActivity.this,android.R.layout.simple_list_item_1, array_sort);
        listView.setAdapter(listAdapter);
        Log.v("itemListViewer","------  BRAVO - done");
    }
    private String[] populateList(){
        Collection<Item> itemList = new ArrayList<Item>();
        ArrayList<String> stringItems=new ArrayList<String>();
        Collection<Member> users=d.getAllUsers();
        Log.v("itemListViewer","------  GAMMA");
        d.close();
        for(Member u: users)
        {
            itemList.addAll(u.getItems());
        }
        Log.v("itemListViewer","------  PI");
        for(Item e:itemList)
        {
            stringItems.add(e.toString());
        }
        String[] finalItems = new String[stringItems.size()];
        for (int i = 0; i < stringItems.size(); i++)
        {
            Log.v("itemListViewer","------  FORE    Adding: " + stringItems.get(i));
            finalItems[i] = stringItems.get(i);
        }
        Log.v("itemListViewer","------  MAVEN    length: " + finalItems.length);
        return finalItems;
    }
}